package com.chb.service.impl;

import com.chb.dao.StaffDao;
import com.chb.model.entity.Staff;
import com.chb.model.vo.Result;
import com.chb.model.vo.StaffVo;
import com.chb.service.UserInfoService;
import com.chb.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

/**
 * @author: 陈海彬
 * @date: Created in 2023/10/9 19:58
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private HttpSession session;
    @Autowired
    private StaffDao staffDao;

    /**
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 获取用户个人信息
     * @create time: 2023/10/9 19:59
     */
    @Override
    public Result getUserInfo() {
        Staff staff = (Staff) session.getAttribute("staff");
        StaffVo userInfo = staffDao.getUserInfo(staff.getStaffid());
        return Result.success(userInfo);
    }


    /**
     * @param uploaded
     * @return void
     * @create by: 陈海彬
     * @description: 修改用户的头像
     * @create time: 2023/10/9 20:57
     */
    @Override
    @Transactional
    public void updateStaffUrl(String uploaded) {
        Staff staff = (Staff) session.getAttribute("staff");
        String url = staff.getUrl();
        staff.setUrl(uploaded);
        //删除原有的图片
        OssUtils.ossRemove(url);

        staffDao.uploadStaffUrl(staff);
    }
}